import { createStore } from 'vuex'

export default createStore({
  state: {
    isCollapse: false, /* 控制菜单收缩 */
    userName: '' || localStorage.getItem('userName'),
    /* 存储Token */
    Authorization: localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : ''
  },
  mutations: {
    collapseMenu (state) { state.isCollapse = !state.isCollapse },
    /* 修改token，并将token存入localStorage */
    changeLogin (state, user) {
      state.Authorization = user.Authorization
      localStorage.setItem('Authorization', user.Authorization)
    },
    /* 把登录的用户的名保存到localStorage中，防止页面刷新，导致store重新启动，用户名就成为初始值（初始值为空）的情况 */
    handleUserName: (state, userName) => {
      state.userName = userName
      localStorage.setItem('userName', userName)
    }
  },
  actions: {},
  modules: {},
  /* getters 只会依赖 state 中的成员去更新 */
  getters: {
    userName: state => state.userName
  }
})
